@echo off
:: Change to the directory where the batch file is located
cd /d "%~dp0"

:: Set paths for the project
set SRC_DIR=src
set BIN_DIR=bin
set LIB_DIR=lib
set ALL_JARS=%LIB_DIR%\gson-2.11.0.jar;%LIB_DIR%\hutool-all-5.8.16.jar;%LIB_DIR%\beautyeye_lnf.jar;%LIB_DIR%\substance.jar

:: Create bin directory if it doesn't exist
if not exist %BIN_DIR% mkdir %BIN_DIR%

:: Compile AI Assistant related files first
echo Compiling AI Assistant files...
javac -cp "%ALL_JARS%" -d %BIN_DIR% -sourcepath %SRC_DIR% %SRC_DIR%\application\Application.java %SRC_DIR%\login\frame\*.java %SRC_DIR%\login\panel\*.java %SRC_DIR%\util\*.java %SRC_DIR%\chat\frame\*.java
if %errorlevel% neq 0 (
    echo AI Assistant compilation failed, but continuing with core weather app...
    :: Create minimal Application class as fallback
    if not exist %BIN_DIR%\application mkdir %BIN_DIR%\application
    (
    echo package application;
    echo public class Application {
    echo     public static void main^(String[] args^) {
    echo         System.out.println^("AI Assistant launched"^);
    echo         javax.swing.JOptionPane.showMessageDialog^(null, "AI Assistant compilation failed. Missing dependencies.", "AI Assistant Error", javax.swing.JOptionPane.ERROR_MESSAGE^);
    echo     }
    echo }
    ) > %BIN_DIR%\application\Application.java
    javac -d %BIN_DIR% %BIN_DIR%\application\Application.java
    del %BIN_DIR%\application\Application.java
) else (
    echo AI Assistant compilation successful!
)

:: Compile core weather application files
echo Compiling core weather application files...
javac -cp "%ALL_JARS%;%BIN_DIR%" -d %BIN_DIR% %SRC_DIR%\WeatherSwingApp.java %SRC_DIR%\WeatherApp.java %SRC_DIR%\WeatherData.java %SRC_DIR%\WeatherAPIService.java %SRC_DIR%\WeatherAPIManager.java %SRC_DIR%\WeatherDataCache.java %SRC_DIR%\UserPreferences.java %SRC_DIR%\MultiAPIManager.java %SRC_DIR%\APISourceInfo.java %SRC_DIR%\APISourceSelector.java %SRC_DIR%\NetworkStatusPanel.java %SRC_DIR%\APIConfigurationPanel.java %SRC_DIR%\EnhancedWeatherPanel.java %SRC_DIR%\ModernUIComponents.java %SRC_DIR%\WeatherAlertListener.java %SRC_DIR%\WeatherAPIComService.java %SRC_DIR%\SocketWeatherAPIService.java %SRC_DIR%\SocketWeatherServer.java %SRC_DIR%\Alert.java %SRC_DIR%\ForecastHistory.java %SRC_DIR%\Location.java %SRC_DIR%\NotificationManager.java %SRC_DIR%\LocationService.java %SRC_DIR%\GeoLocationData.java %SRC_DIR%\WeatherTrendChart.java
if %errorlevel% neq 0 (
    echo Core compilation failed.
    pause
    exit /b
)

echo Compilation successful!

:: Run the application
echo Running Weather Application...
java -cp "%BIN_DIR%;%ALL_JARS%" WeatherSwingApp
if %errorlevel% neq 0 (
    echo Application encountered an error.
    pause
    exit /b
)

pause
